# -*- coding: utf-8 -*-
"""
@Time ： 2024/2/6 14:00
@Auth ： gc
"""
import pandas as pd
import akshare as ak

def get_hs300():
    """
    获取沪深300成信息
    """
    codes = pd.read_csv("stock/data/hs300_stocks.csv", converters = {"code":str})["code"].values.tolist()
    l = []
    c = None
    for i, code in enumerate(codes):
        stock_individual_info_em_df = ak.stock_individual_info_em(symbol = code)
        if i == 0:
            c = stock_individual_info_em_df["item"].values.tolist()
        l.append(stock_individual_info_em_df["value"].values.tolist())
    stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
    # stock_zh_a_spot_em_df[stock_zh_a_spot_em_df["代码"].isin(codes)].to_excel("./data/hs300_stocks_info_2.xlsx", index=False)
    df = pd.merge(pd.DataFrame(l, columns = c)[["行业", "上市时间", "股票代码", "股票简称", "总股本", "流通股"]],
                  stock_zh_a_spot_em_df[stock_zh_a_spot_em_df["代码"].isin(codes)], left_on = '股票代码', right_on = '代码',
                  how = 'left')
    df.to_excel("stock/data/hs300_stocks_info.xlsx", index = False)
